﻿using System;
using System.Web;
using Bingosoft.Security;

namespace Bingosoft.WebApp.Layouts
{
    public partial class Layout : System.Web.UI.MasterPage
    {
        public string menus = "";

        protected string UserName { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            UserName = SecurityContext.User.Name;

            menus = "[";

            var isFirst = true;
            if (SiteMapDataSource.Provider.RootNode != null)
                foreach (SiteMapNode item in SiteMapDataSource.Provider.RootNode.ChildNodes)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        menus = menus + ",";
                    }
                    menus = menus + GetMenu(item);
                }

            menus = menus + "]";

        }

        private static string GetMenu(SiteMapNode node)
        {
            var menu = "{";

            string id = Guid.NewGuid().ToString();

            menu = (menu + "id:'" + id + "',url:'" + node.Url + "',text:'" + node.Title + "'");

            if (node.HasChildNodes)
            {
                menu = menu + ",childNode:[";
                var isFirst = true;

                foreach (SiteMapNode item in node.ChildNodes)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        menu = menu + ",";
                    }
                    menu = menu + GetMenu(item);
                }
                menu = menu + "]";
            }

            menu = menu + "}";

            return menu;
        }
    }
}